#include "DeviceInfoBLL.h"
#include <string.h>
#include "flash.h"


static DeviceInfo *pDeviceInfo;
static SystemRunTimeInfo *pSysRunInfo;
//读取app的版本信息的
//注意，读取app的版本信息仅读取
//软件版本就行，从bootloader的
//machineversion读取硬件信息
//因为硬件一旦确定了就不改了
//而bootloader一般也是烧写之后也不会再该
//而app的版本信息有可能修改，这个是
//和app相关的，所以这里读取app的machineversion
static LocalMachineVersion *pLocalMachineVersion;			

static void SaveDeviceInfo(void)
{
	STMFLASH_Write(DEVICEINFO_ADDR, (uint32_t *)pDeviceInfo, sizeof(DeviceInfo) / sizeof(uint32_t));
}

static void FirstStartInitDevice(void)
{
	char *update_time = "20220920 14:25:25";
	char *model_sn = "XPDHYZJBMS02401P24S2024052500001";
	char *default_password = "xpdftpwd";
	
	strcpy((char *)pDeviceInfo->update_datetime, update_time);
	strcpy((char *)pDeviceInfo->model_sn, model_sn);
	strcpy((char *)pDeviceInfo->login_password, default_password);
	
	memcpy(pDeviceInfo->model_name, MODEL_NAME, strlen(MODEL_NAME));
	
	pDeviceInfo->update_flag = true;
	pDeviceInfo->try_startup_times = 0;
	
	pDeviceInfo->first_startup = false;
	
	SaveDeviceInfo();
}

bool GetUpdateFlag(void)
{
	if(pDeviceInfo != NULL)
		return pDeviceInfo->update_flag;
	else
		return false;
}

void InitDeviceInfo(void)
{
	pDeviceInfo = GetDeviceInfo();
	pSysRunInfo = GetSysRunTimeInfo();
	pLocalMachineVersion = GetAppMachineVersion();
	
	STMFLASH_Read (DEVICEINFO_ADDR, (uint32_t*)pDeviceInfo, 
			sizeof(DeviceInfo) / sizeof(uint32_t));
	
	if(pDeviceInfo->first_startup)
	{
		FirstStartInitDevice();
		STMFLASH_Read(DEVICEINFO_ADDR, (uint32_t*)pDeviceInfo, 
				sizeof(DeviceInfo) / sizeof(uint32_t));
	}

}

void GetDeviceSoftwareInfo(SoftwareVersion *soft_vers)
{
	memcpy(soft_vers, &(pLocalMachineVersion->sfv), sizeof(LclMchVers.sfv));
	//memcpy(soft_vers, &(LclMchVers.sfv), sizeof(LclMchVers.sfv));
}

void GetDeviceHardwareInfo(HardwareVersion *hard_vers)
{
//	memcpy(hard_vers, &(pLocalMachineVersion->hdv), sizeof(LclMchVers.hdv));
	memcpy(hard_vers, &(LclMchVers.hdv), sizeof(LclMchVers.hdv));
}

void SetDeviceModelName(uint8_t *model_name, uint8_t length, bool needsave)
{
	memcpy(pDeviceInfo->model_name, model_name, length);
	if(needsave)
		SaveDeviceInfo();
}

uint8_t GetDeviceModelName(uint8_t *model_name)
{
	uint8_t model_name_length = strlen(pDeviceInfo->model_name);
	
	memcpy(model_name, pDeviceInfo->model_name, model_name_length);
	
	return model_name_length;
}

void SetDeviceUpdateTime(uint8_t *update_time, bool needsave)
{
	memcpy(pDeviceInfo->update_datetime, update_time, sizeof(pDeviceInfo->update_datetime));
	if(needsave)
		SaveDeviceInfo();
}

void GetDeviceUpdateTime(uint8_t *update_time)
{
	memcpy(update_time, pDeviceInfo->update_datetime, sizeof(pDeviceInfo->update_datetime));
}

void SetDeviceUpdateFlag(bool update, bool needsave)
{
	pDeviceInfo->update_flag = update;
	if(needsave)
		SaveDeviceInfo();
}

bool GetDeviceUpdateFlag(void)
{
	return pDeviceInfo->update_flag;
}

void SetDeviceTryStartTimes(uint8_t times, bool needsave)
{
	pDeviceInfo->try_startup_times = times;
	if(needsave)
		SaveDeviceInfo();
}

uint8_t GetDeviceTryStartTimes(void)
{
	return pDeviceInfo->try_startup_times;
}

bool IsFirstStartup(void)
{
	return pDeviceInfo->first_startup;
}

void SetFirtStartup(bool first_startup, bool needsave)
{
	pDeviceInfo->first_startup = first_startup;
	if(needsave)
		SaveDeviceInfo();
}

uint16_t GetSystemModelNum(void)
{
	return pSysRunInfo->u16ModelID;
}

void SetSystemModelNum(uint16_t mID)
{
	pSysRunInfo->u16ModelID = mID;
}

void SetFirmwareInfo(uint32_t size, uint16_t crc, bool needsave)
{
	pDeviceInfo->firmware_size = size;
	pDeviceInfo->firmware_crc = crc;
	
	if(needsave)
		SaveDeviceInfo();
	
}

void GetFirmwareInfo(uint32_t *size, uint16_t *crc)
{
	*size = pDeviceInfo->firmware_size;
	*crc = pDeviceInfo->firmware_crc;
}
